package com.dw.util;

import com.dw.model.user.User;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.net.URL;

public class UIUtil {
    public static final Color PRIMARY_COLOR = new Color(70, 130, 180); // 蓝色主色调
    public static final Color SECONDARY_COLOR = new Color(245, 245, 250); // 浅灰蓝色次要色调
    public static final Color TEXT_COLOR = new Color(50, 50, 50); // 深灰色文本
    public static final Color ERROR_COLOR = new Color(220, 0, 0); // 错误提示色
    public static final Color SUCCESS_COLOR = new Color(0, 128, 0); // 成功提示色

    public static final Font TITLE_FONT = new Font("微软雅黑", Font.BOLD, 18);
    public static final Font SUBTITLE_FONT = new Font("微软雅黑", Font.BOLD, 16);
    public static final Font NORMAL_FONT = new Font("微软雅黑", Font.PLAIN, 14);
    public static final Font SMALL_FONT = new Font("微软雅黑", Font.PLAIN, 12);

    public static final Border PANEL_BORDER = BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(PRIMARY_COLOR, 1),
            BorderFactory.createEmptyBorder(10, 10, 10, 10)
    );

    public static void setDefaultStyle(JComponent component) {
        component.setFont(NORMAL_FONT);
        component.setForeground(Color.BLACK);

        if (component instanceof JButton button) {
            button.setBackground(PRIMARY_COLOR);
            button.setForeground(Color.BLACK);
            button.setFocusPainted(false);
            button.setBorderPainted(true);
            button.setOpaque(true);
        } else if (component instanceof JTextField || component instanceof JPasswordField || component instanceof JTextArea) {
            component.setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(PRIMARY_COLOR, 1),
                    BorderFactory.createEmptyBorder(5, 5, 5, 5)
            ));
        } else if (component instanceof JPanel) {
            component.setBorder(PANEL_BORDER);
            component.setBackground(Color.WHITE);
        } else if (component instanceof JLabel) {
            component.setForeground(Color.BLACK);
        } else if (component instanceof JComboBox) {
            component.setBorder(BorderFactory.createLineBorder(PRIMARY_COLOR, 1));
        }
    }

    public static ImageIcon getIcon(String path) {
        URL url = UIUtil.class.getResource(path);
        if (url != null) {
            return new ImageIcon(url);
        }
        return null;
    }

    public static JButton createButton(String text) {
        JButton button = new JButton(text);
        setDefaultStyle(button);
        button.setForeground(Color.BLACK);
        return button;
    }

    public static JLabel createLabel(String text) {
        JLabel label = new JLabel(text);
        setDefaultStyle(label);
        return label;
    }

    public static JLabel createTitleLabel(String text) {
        JLabel label = new JLabel(text);
        label.setFont(TITLE_FONT);
        label.setForeground(PRIMARY_COLOR);
        return label;
    }

    public static JTextField createTextField() {
        JTextField textField = new JTextField();
        setDefaultStyle(textField);
        return textField;
    }

    public static JTextArea createTextArea(int row, int column) {
        JTextArea textArea = new JTextArea(row, column);
        setDefaultStyle(textArea);
        return textArea;
    }

    public static JPasswordField createPasswordField() {
        JPasswordField passwordField = new JPasswordField();
        setDefaultStyle(passwordField);
        return passwordField;
    }

    public static JComboBox<String> createComboBox(String[] items) {
        JComboBox<String> comboBox = new JComboBox<>(items);
        setDefaultStyle(comboBox);
        return comboBox;
    }

    public static JPanel createPanel() {
        JPanel panel = new JPanel();
        setDefaultStyle(panel);
        return panel;
    }

    public static JTable createTable(AbstractTableModel model) {
        JTable table = new JTable(model);
        setDefaultStyle(table);
        return table;
    }


    public static void showInfo(Component parent, String message, String title) {
        JOptionPane.showMessageDialog(parent, message, title, JOptionPane.INFORMATION_MESSAGE);
    }

    public static void showError(Component parent, String message, String title) {
        JOptionPane.showMessageDialog(parent, message, title, JOptionPane.ERROR_MESSAGE);
    }

    public static boolean showConfirm(Component parent, String message, String title) {
        int result = JOptionPane.showConfirmDialog(parent, message, title, JOptionPane.YES_NO_OPTION);
        return result == JOptionPane.YES_OPTION;
    }
}